﻿using System;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using System.Xml.Linq;
using System.Xml.Serialization;
using JCore.LoLProfiler_Test.Classes;
using JCore.LoLProfiler_Test.Classes.Objects;

namespace JCore.LoLChat.Classes
{
    public class XMLParser
    {
        public static Database ReadFromXml()
        {
            if (File.Exists("Database.xml"))
            {
                XmlSerializer serializer = new XmlSerializer(typeof (Database));
                Database database = (Database) serializer.Deserialize(new StreamReader(@"Database.xml"));
                database.Summoner.LoginInfo.Password = PasswordEncrypt.Decrypt(database.Summoner.LoginInfo.Password);
                return database;
            }
            return null;
        }

        public static void WriteDatabase(Summoner data)
        {
            XElement Database =
                new XElement("Database",
                             new XElement("VersionNumber", Application.ProductVersion),
                             new XElement("RevisionData", DateTime.Now),
                             new XElement("Summoner",
                                          new XElement("LoginInfo",
                                                       new XElement("LoginName", data.LoginInfo.LoginName),
                                                       new XElement("Password", data.LoginInfo.Password)),
                                          new XElement("SummonerInfo",
                                                       new XElement("Name", data.SummonerInfo.Name),
                                                       new XElement("AccountId", data.SummonerInfo.AccountId),
                                                       new XElement("RevisionDate", data.SummonerInfo.RevisionDate),
                                                       new XElement("SummonerId", data.SummonerInfo.SummonerId),
                                                       new XElement("Friends",
                                                                    from friend in data.SummonerInfo.Friends
                                                                    select new XElement("Friend",
                                                                                        new XElement("Name", friend.Name),
                                                                                        new XElement("AccountId", friend.AccountId),
                                                                                        new XElement("RevisionDate", friend.RevisionDate),
                                                                                        new XElement("SummonerId",
                                                                                                     friend.SummonerId)
                                                                        )

                                                           )
                                              )
                                              
                                              ,
                                          new XElement("Matches",
                                                       from match in data.Matches
                                                       select new XElement("Match",
                                                                           new XElement("Won", match.Won),
                                                                           new XElement("Side", match.Side),
                                                                           new XElement("Ranked", match.Ranked),
                                                                           new XElement("GameMode", match.GameMode),
                                                                           new XElement("SubType", match.SubType),
                                                                           new XElement("Players",
                                                                                        from player in match.Players
                                                                                        select new XElement("Player",
                                                                                                            new XElement
                                                                                                                ("Name",
                                                                                                                 player
                                                                                                                     .Name),
                                                                                                            new XElement
                                                                                                                ("Champion",
                                                                                                                 player
                                                                                                                     .Champion),
                                                                                                            new XElement
                                                                                                                ("Kills",
                                                                                                                 player
                                                                                                                     .Kills),
                                                                                                            new XElement
                                                                                                                ("Deaths",
                                                                                                                 player
                                                                                                                     .Deaths),
                                                                                                            new XElement
                                                                                                                ("Assists",
                                                                                                                 player
                                                                                                                     .Assists),
                                                                                                            new XElement
                                                                                                                ("Score",
                                                                                                                 player
                                                                                                                     .Score))
                                                                               )
                                                           )
                                              )
                                 ));
            Database.Save("Database.xml");
            MessageBox.Show("Database complete!");
        }
    }
}
